package org.article;

import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PGraphics;

public abstract class Article{
	// Sections of text
	protected ArrayList<Section> articleSections;
	
	// Coordinates (x, y, width, height) 
	protected float articleX;
	protected float articleY;
	protected float articleW; 
	protected float articleH;
	
	protected float drawingArea;
	
	// The padding of the drawing object (to avoid visual collisions with the sections)
	protected int articleDrawingPadding = 7;
	
	// Indicator: the current status of the system (transcluding or not transcluding)
	protected boolean inTransclusion;	
	
	// PApplet parent
	protected PApplet parent;
	
	// PGraphics parent
	protected PGraphics pgDocuments;
	protected PGraphics pgPreview;
	
	// Abstract methods
	public abstract void resetStatus();
	
	/**
	 * Constructor with parameter width and NO position
	 * @param articleWidth
	 * @param parent PApplet parent.
	 */
	public Article(float articleWidth, float articleHeight, PApplet parent, PGraphics pgDocuments, PGraphics pgPreview) {
		articleSections = new ArrayList<Section>();

		articleW = articleWidth;
		
		articleH = articleHeight;
		
		this.parent = parent;
		
		this.pgDocuments = pgDocuments;
		
		this.pgPreview = pgPreview;
	}
	
	/**
	 * DRAW Method.
	 * It is mandatory to set the position of the document BEFORE using this method
	 */
	public void draw(){
		// TODO: Maybe delete? this color assignment
		pgDocuments.fill(196);
		
		// The rectangle over the document is drawn		
		pgDocuments.stroke(64);
		pgDocuments.rect (articleX, articleY, articleW, articleH);
		pgDocuments.stroke(0);
		
		// Now the sections are drawn, one by one
		drawSectionsFromArticle();
	}
	
	/**
	 * Method that draws the sections of this Document.
	 * 		The position is calculated with these rules:
	 * 			posX = posXDocument + padding
	 * 
	 * 			posY0 = posYDocument + padding
	 * 			posYN = posY(N-1) + padding
	 * 		THE PADDING IS ADDED BY THE DRAW METHOD OF THE SECTION!!!!
	 */
	private void drawSectionsFromArticle(){
		calculateSectionsSize();
		
		int numberOfSections = articleSections.size();
		Section tempSection;

		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			// The PGraphcs are given to the Sections
			tempSection.setPAppletAndPGraphics(parent, pgDocuments, pgPreview);
			
			 // (INSIDE THE CLASS, it is checked for the mouse position, to know when to change color)
			tempSection.draw();
		}
	}
	
	/**
	 * Method that determines the size of the document, according to the sections size
	 * The size of the document equals to:
	 * 		Document = (Section1 + P1) + ... + (SectionK +Pk) + P(k+1)
	 * 	Letting P be the padding between sections
	 */
	private void calculateSectionsSize(){
		int numberOfSections = articleSections.size();
		Section tempSection;

		drawingArea = articleH - ((numberOfSections + 1) * articleDrawingPadding);
		
		float sectionX, sectionY, sectionW, sectionH;
		
		float sumOfSectionSize = 0;
		
		// Initial coordinates and dimensions
		sectionX = articleX + articleDrawingPadding;
		sectionY = articleY + articleDrawingPadding;
		sectionW = articleW - (2 * articleDrawingPadding); // The document width minus the 'margins'
		
		for(int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			sumOfSectionSize = sumOfSectionSize + tempSection.calculateSectionHeight();
		}
		
		for(int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			sectionH = (tempSection.calculateSectionHeight() / sumOfSectionSize) * drawingArea;
			
			tempSection.setSectionDimensions(sectionX, sectionY, sectionW, sectionH);
			
			// The new Y is determined
			sectionY = sectionY + tempSection.getSectionSize() + articleDrawingPadding;
		}
	}
	
	/**
	 * Method that adds a Section to the current document
	 * It sets the PApplet and PGraphics elements
	 * @param section
	 */
	public void addNewSection(Section section) {
		section.setPAppletAndPGraphics(parent, pgDocuments, pgPreview);
		
		articleSections.add(section);
	}

	public void setTransclusionStatus(boolean status){
		this.inTransclusion = status;
	}
	
	/**
	 * Method that checks if a click was made on this document, specifically ON THE SECTIONS.
	 * @return
	 */
	public boolean isMouseOverAnySection(){
		int numberOfSections = articleSections.size();
		boolean isOver, result;
		
		result = false;
		
		Section tempSection;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			isOver = tempSection.checkForMouseOver();
			
			if(isOver) {
				result = isOver; 
			}
		}
		
		return result;
	}
	

	/**
	 * Method that sets the position of drawing of a document
	 * @param posX
	 * @param 
	 */
	public void setArticlePosition(float posX, float posY){
		articleX = posX;
		articleY = posY;
	}
	
	/**
	 * Method that checks if there is a section that was pressed
	 * @param posX mouse position in x axis
	 * @param posY mouse position in y axis
	 */
	public String getTextFromSectionIn(int posX, int posY) {
		int numberOfSections = articleSections.size();
		boolean isOver = false;
		Section tempSection;
		String sectionContent = "";
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			isOver = tempSection.checkForMouseOver();
			
			if(isOver) {
				sectionContent = tempSection.getSectionText(); 
			}
		}
		
		return sectionContent;
	}
}
